package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.enums.LabelTypeEnum;
import com.caifu.pojo.YsShop;
import com.caifu.pojo.YsShopLicence;
import com.caifu.service.ShopService;
import com.caifu.util.CheckUtil;
import com.caifu.vo.ShopPageVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * ClassName : ShopController
 *
 * @author :  yb
 * @description ：
 * @date : 2021-03-02 17:16
 */
@Controller
@RequestMapping(value = "/shop")
public class ShopController {

    @Resource
    private ShopService shopService;


    /**
     * 跳转 商铺列表 页面
     * @return ModelAndView
     */
    @GetMapping(value = "jumpShopListPage")
    public ModelAndView jumpShopListPage(){
        ModelAndView modelAndView = new ModelAndView();
        try {
            setParamModelAndView(modelAndView);
            modelAndView.setViewName("shop/shop_list");
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("error/error");
        }
        return modelAndView;
    }

    private void setParamModelAndView(ModelAndView modelAndView) {
        //经营状态
        modelAndView.addObject("managementStatusList", shopService.getYsLabelList(LabelTypeEnum.JYZT.getKey()));
        //经营业态
        modelAndView.addObject("businessTypeList", shopService.getYsLabelList(LabelTypeEnum.JYYT.getKey()));
        //商铺标签
        modelAndView.addObject("shopTypeList", shopService.getYsLabelList(LabelTypeEnum.SPBQ.getKey()));
        //证照类型
        modelAndView.addObject("licenceTypeList", shopService.getYsLabelList(LabelTypeEnum.ZZLX.getKey()));
    }




    /**
     * 商铺列表
     *
     * @param vo reportRecordPageVO
     * @return Result
     */
    @GetMapping(value = "getShopListPage")
    @ResponseBody
    public Result<Object> getShopListPage(ShopPageVO vo) {
        try {
            //状态 1-有效 2-无效 8-关闭 9-删除
            vo.setState(1);
            return shopService.getShopListPage(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 商铺 关闭列表
     *
     * @param vo reportRecordPageVO
     * @return Result
     */
    @GetMapping(value = "getShopCloseListPage")
    @ResponseBody
    public Result<Object> getShopCloseListPage(ShopPageVO vo) {
        try {
            //状态 1-有效 2-无效 8-关闭 9-删除
            vo.setState(8);
            return shopService.getShopListPage(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }



    /**
     * 跳转 商铺 关闭列表  页面
     * @return ModelAndView
     */
    @GetMapping(value = "jumpShopClosePage")
    public ModelAndView jumpShopClosePage(){
        ModelAndView modelAndView = new ModelAndView();
        try {
            setParamModelAndView(modelAndView);
            modelAndView.setViewName("shop/shop_shop_close_list");
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("error/error");
        }
        return modelAndView;
    }


    /**
     * 删除 商铺
     *
     * @param id id
     * @return Result
     */
    @PostMapping(value = "deleteShop")
    @ResponseBody
    public Result<Object> deleteShop(String id) {
        try {
            if (CheckUtil.isNull(id)) {
                return ResultUtil.errorMsg("删除商铺条件有误,请重新操作");
            }
            return shopService.deleteShop(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 关闭 商铺
     * @param id id
     * @return Result
     */
    @PostMapping(value = "closeShop")
    @ResponseBody
    public Result<Object> closeShop(String id) {
        try {
            if (CheckUtil.isNull(id)) {
                return ResultUtil.errorMsg("关闭商铺条件有误,请重新操作");
            }
            return shopService.closeShop(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 跳转 商铺 登记页面
     * @return ModelAndView
     */
    @GetMapping(value = "jumpShopRegister")
    public ModelAndView jumpShopRegister(){
        ModelAndView modelAndView = new ModelAndView();
        try {
            setParamModelAndView(modelAndView);
            modelAndView.setViewName("shop/shop_register_list");
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("error/error");
        }
        return modelAndView;
    }




    /**
     * 跳转 商铺 添加
     * @return ModelAndView
     */
    @GetMapping(value = "jumpShopAdd")
    public ModelAndView jumpShopAdd(){
        ModelAndView modelAndView = new ModelAndView();
        try {
            setParamModelAndView(modelAndView);
            modelAndView.setViewName("shop/shop_add");
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("error/error");
        }
        return modelAndView;
    }




    /**
     * 添加 商铺
     *
     * @param shop shop
     * @return Result
     */
    @PostMapping(value = "addShop")
    @ResponseBody
    public Result<Object> addShop(YsShop shop) {
        try {

            return shopService.addShop(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 修改 商铺
     *
     * @param shop shop
     * @return Result
     */
    @PostMapping(value = "updateShop")
    @ResponseBody
    public Result<Object> updateShop(YsShop shop) {
        try {

            return shopService.updateShop(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 跳转 商铺 详情
     * @return ModelAndView
     */
    @GetMapping(value = "jumpShopDetail")
    public ModelAndView jumpShopDetail(String id){
        ModelAndView modelAndView = new ModelAndView();
        try {
            modelAndView.addObject("shop", shopService.getShopDetailById(id));
            modelAndView.setViewName("shop/shop_detail");
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("error/error");
        }
        return modelAndView;
    }


    /**
     * 跳转 商铺  修改
     * @return ModelAndView
     */
    @GetMapping(value = "jumpShopUpdate")
    public ModelAndView jumpShopUpdate(String id){
        ModelAndView modelAndView = new ModelAndView();
        try {
            setParamModelAndView(modelAndView);
            modelAndView.addObject("shopId", id);
            modelAndView.setViewName("shop/shop_update");
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("error/error");
        }
        return modelAndView;
    }


    /**
     * 获取 商铺
     *
     * @param id id
     * @return Result
     */
    @PostMapping(value = "getYsShopById")
    @ResponseBody
    public Result<Object> getYsShopById(String id) {
        try {
            return ResultUtil.getDataSuccess(shopService.getShop(id));
        } catch (Exception e) {

            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 添加 商铺 证照信息
     *
     * @return Result
     */
    @PostMapping(value = "addOrUpdateShopLicence")
    @ResponseBody
    public Result<Object> addOrUpdateShopLicence(YsShopLicence ysShopLicence) {
        try {

            return shopService.addOrUpdateShopLicence(ysShopLicence);
        } catch (Exception e) {

            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 删除 证照
     *
     * @return Result
     */
    @PostMapping(value = "deleteLicence")
    @ResponseBody
    public Result<Object> deleteLicence(String id) {
        try {

            return shopService.deleteLicence(id);
        } catch (Exception e) {

            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 获取 证照 详情
     *
     * @return Result
     */
    @PostMapping(value = "getLicenceById")
    @ResponseBody
    public Result<Object> getLicenceById(String id) {
        try {

            return shopService.getLicenceById(id);
        } catch (Exception e) {

            e.printStackTrace();
            return ResultUtil.error();
        }
    }



}
